package com.duan.goods.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.duan.goods.pojo.Spec;
import com.duan.goods.service.SpecService;
import com.duan.goods.mapper.SpecMapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
* @author lenovo
* @description 针对表【goods_spec(规格表)】的数据库操作Service实现
* @createDate 2024-07-28 16:20:08
*/
@Service
public class SpecServiceImpl extends ServiceImpl<SpecMapper, Spec>
    implements SpecService{
    @Autowired
    private SpecMapper specMapper;

    @Override
    public List<Spec> selectAll() {
        LambdaQueryWrapper<Spec> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(Spec::getIsDelete,0);
        return specMapper.selectList(queryWrapper);
    }

    @Override
    public Integer insert(Spec spec) {
        return specMapper.insert(spec);
    }

    @Override
    public Integer update(Spec spec) {
        return specMapper.updateById(spec);
    }

    @Override
    @Transactional
    public Integer delete(String ids) {
        try {
            String[] split = ids.split(",");
            for (String id : split) {
                Spec spec=new Spec();
                spec.setId(Long.parseLong(id));
                spec.setIsDelete(1);
                specMapper.updateById(spec);
            }
            return 1;
        }catch (Exception e){
            return 0;
        }
    }

    @Override
    public List<Spec> selectList(Spec spec) {
        LambdaQueryWrapper<Spec> queryWrapper=queryWrapper(spec);
        return specMapper.selectList(queryWrapper);
    }

    @Override
    public Page<Spec> pageAll(Integer page, Integer size) {
        PageHelper.startPage(page,size);
        return (Page<Spec>) this.selectAll();
    }

    @Override
    public Page<Spec> pageList(Integer page, Integer size, Spec spec) {
        PageHelper.startPage(page,size);
        return (Page<Spec>) this.selectList(spec);
    }

    public LambdaQueryWrapper<Spec> queryWrapper(Spec spec){
        LambdaQueryWrapper <Spec> queryWrapper = new LambdaQueryWrapper<>();
        if (spec.getId() != null){
            queryWrapper.eq(Spec::getId,spec.getId());
        }
        if (spec.getSpecName() != null){
            queryWrapper.eq(Spec::getSpecName,spec.getSpecName());
        }
        if (spec.getSpecValue() != null){
            queryWrapper.eq(Spec::getSpecValue,spec.getSpecValue());
        }
        if (spec.getIsDelete() != null){
            queryWrapper.eq(Spec::getIsDelete,spec.getIsDelete());
        }
        if (spec.getCreateTime() != null){
            queryWrapper.eq(Spec::getCreateTime,spec.getCreateTime());
        }
        if (spec.getUpdateTime() != null){
            queryWrapper.eq(Spec::getUpdateTime,spec.getUpdateTime());
        }
        return queryWrapper;
    }

}




